#include <stdio.h>
#include "basiciff.h"

//Ower own struct
typedef struct {
  ClientFrame clientFrame;
  int levels;		/* # groups currently nested within.*/
} MyFrame;


void PutHdr(GroupContext *context) {
  IFFP id = context->ckHdr.ckID;
  printf("level=$%X %c%c%c%c $%X size=$%X\n", 
	 ((MyFrame *)(context->clientFrame))->levels,
	 (char)( id >> 24),
	 (char)((id >> 16) & 0x7f),
	 (char)((id >> 8)  & 0x7f),
	 (char)( id        & 0x7f),
	 id,
	 context->ckHdr.ckSize);
  if (context->subtype != NULL_CHUNK) {
    IFFP id = context->subtype;
    printf("\t %c%c%c%c = subtype $%X\n",
	   (char)( id >> 24),
	   (char)((id >> 16) & 0x7f),
	   (char)((id >> 8)  & 0x7f),
	   (char)( id        & 0x7f),
	   id);
  }
}


/* ---------- AtLeaf ---------------------------------------------------*/
/* At Leaf chunk.  That is, a chunk which does NOT contain other chunks.
 * Print "ID size".*/
IFFP AtLeaf(GroupContext *context) {
  PutHdr(context);
  /* A typical reader would read the chunk's contents, using the "Frame"
   * for local data, esp. shared property settings (PROP).*/
  /* IFFReadBytes(context, ...buffer, context->ckHdr->ckSize); */
  return(IFF_OKAY);
}


/* ---------- GetForm --------------------------------------------------*/
/* Handle a FORM chunk.  Print "FORM size subTypeID".
 * Then dive into it.*/
IFFP GetForm(GroupContext *parent) {
  IFFP iffp;
  GroupContext new;
  MyFrame newFrame;

  newFrame = *(MyFrame *)parent->clientFrame;  /* copy parent's frame*/
  newFrame.levels++;
  PutHdr(parent);

  iffp = OpenRGroup(parent, &new);
  CheckIFFP();
  new.clientFrame = (ClientFrame *)&newFrame;

  /* FORM reader for Checker. */
  /* LIST, FORM, PROP, CAT already handled by GetF1ChunkHdr. */
  do {
    if ((iffp = GetF1ChunkHdr(&new)) > 0) {
      iffp = AtLeaf(&new);
    }
  } while (iffp >= IFF_OKAY);
  
  CloseRGroup(&new);
  return(iffp == END_MARK ? IFF_OKAY : iffp);
}


int main() {
  FILE *f;
  GroupContext gc;
  IFFP iffp;
  MyFrame frame = {
    {SkipGroup, SkipGroup, SkipGroup, SkipGroup},
    0
  };

  f = fopen("testfile.iff", "rb");
  iffp = ReadIFF(f, (ClientFrame *)&frame);
  printf("iffp = %d $%X\n", iffp, iffp);
  frame.clientFrame.getForm = &GetForm;
  iffp = ReadIFF(f, (ClientFrame *)&frame);
  printf("iffp = %d $%X\n", iffp, iffp);
  
  /* frame.clientFrame.getList = GetList; */
  /* frame.clientFrame.getForm = GetForm; */
  /* frame.clientFrame.getProp = GetProp; */
  /* frame.clientFrame.getCat  = GetCat ; */

  /*   printf("----- Checking file '%s' -----\n", name); */
  /*   if (file == 0) */
  /* 	iffp = NO_FILE; */
  /*   else */
  /* 	iffp = ReadIFF(file, (ClientFrame *)&frame); */
  
  fclose(f);
  return 0;
}
